﻿Public Class TestFile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim DataPartida As Date = Now.AddMonths(-1)

        If Not Me.IsPostBack Then
            Me.txtDataInici.Text = New Date(DataPartida.Year, DataPartida.Month, 1).ToShortDateString
            Me.txtDataFi.Text = New Date(DataPartida.Year, DataPartida.Month, 1).AddMonths(1).AddDays(-1).ToShortDateString
        End If
    End Sub

    Private Function ValidarDates() As Boolean
        Dim DataInicial As Date
        Dim DataFinal As Date

        If Not Date.TryParse(txtDataInici.Text, DataInicial) Then
            Response.Write("Data Inicial No Válida")
            Return False
        End If

        If Not Date.TryParse(txtDataFi.Text, DataFinal) Then
            Response.Write("Data Final No Válida")
            Return False
        End If

        Return True
    End Function


    Protected Sub Button1_ServerClick(Sender As Object, e As System.EventArgs)
        Dim LListaHostals As List(Of Hostal)
        Dim LListaUsuaris As List(Of Usuari)
        Dim DataInicial As Date
        Dim DataFinal As Date

        If Not ValidarDates() Then Return

        DataInicial = Date.Parse(txtDataInici.Text)
        DataFinal = Date.Parse(txtDataFi.Text)

        Using Entities As LullabyEntities = New LullabyEntities
            LListaHostals = (From H As Hostal In Entities.Hostals Where H.Id = Me.cboHostals.SelectedValue Or Me.cboHostals.SelectedValue = -1).ToList()
            LListaUsuaris = (From U As Usuari In Entities.Usuaris Where U.Id = Me.cboUsuaris.SelectedValue Or Me.cboUsuaris.SelectedValue = -1).ToList()
        End Using

        Response.Clear()
        Response.ContentType = "application/xlsx"
        Response.AddHeader("content-disposition", "attachment; filename=ControlCajaHostales.xlsx")
        Response.WriteFile(ExcelControlDeCaixaHostals.Generar(LListaHostals, LListaUsuaris, DataInicial, DataFinal))
        Response.Flush()
        Response.End()
    End Sub

    Protected Sub Button2_ServerClick(Sender As Object, e As System.EventArgs)
        Dim LListaHostals As List(Of Hostal)
        Dim DataInicial As Date
        Dim DataFinal As Date

        If Not ValidarDates() Then Return

        DataInicial = Date.Parse(txtDataInici.Text)
        DataFinal = Date.Parse(txtDataFi.Text)

        Using Entities As LullabyEntities = New LullabyEntities
            LListaHostals = (From H As Hostal In Entities.Hostals Where H.Id = Me.cboHostals.SelectedValue Or Me.cboHostals.SelectedValue = -1).ToList()
        End Using

        Response.Clear()
        Response.ContentType = "application/xlsx"
        Response.AddHeader("content-disposition", "attachment; filename=ResumenDiario.xlsx")
        Response.WriteFile(ExcelResumDiari.Generar(LListaHostals, DataInicial, DataFinal))
        Response.Flush()
        Response.End()
    End Sub

    Protected Sub Button3_ServerClick(Sender As Object, e As System.EventArgs)
        Dim LListaUsuaris As List(Of Usuari)
        Dim DataInicial As Date
        Dim DataFinal As Date

        If Not ValidarDates() Then Return

        DataInicial = Date.Parse(txtDataInici.Text)
        DataFinal = Date.Parse(txtDataFi.Text)

        Using Entities As LullabyEntities = New LullabyEntities
            LListaUsuaris = (From U As Usuari In Entities.Usuaris Where U.Id = Me.cboUsuaris.SelectedValue Or Me.cboUsuaris.SelectedValue = -1).ToList()
        End Using

        Response.Clear()
        Response.ContentType = "application/xlsx"
        Response.AddHeader("content-disposition", "attachment; filename=HorasTrabajadas.xlsx")
        Response.WriteFile(ExcelHoresTreballades.Generar(LListaUsuaris, DataInicial, DataFinal))
        Response.Flush()
        Response.End()
    End Sub

    Protected Sub Button4_ServerClick(Sender As Object, e As System.EventArgs)
        Dim LListaHostals As List(Of Hostal)
        Dim DataInicial As Date
        Dim DataFinal As Date

        If Not ValidarDates() Then Return

        DataInicial = Date.Parse(txtDataInici.Text)
        DataFinal = Date.Parse(txtDataFi.Text)

        Using Entities As LullabyEntities = New LullabyEntities
            LListaHostals = (From H As Hostal In Entities.Hostals Where H.Id = Me.cboHostals.SelectedValue Or Me.cboHostals.SelectedValue = -1).ToList()
        End Using

        Response.Clear()
        Response.ContentType = "application/xlsx"
        Response.AddHeader("content-disposition", "attachment; filename=DetalleGastosIngresos.xlsx")
        Response.WriteFile(ExcelDetallGastosIngressos.Generar(LListaHostals, DataInicial, DataFinal))
        Response.Flush()
        Response.End()
    End Sub
End Class

Public Class DadesTestFile
    Public Function GetHostals() As List(Of Hostal)
        Dim LLista As List(Of Hostal)

        Using Entities As LullabyEntities = New LullabyEntities()
            LLista = Entities.Hostals.OrderBy(Function(f) f.Nom).ToList()
            LLista.Insert(0, New Hostal() With {.Id = -1, .Nom = "Tots"})
        End Using

        Return LLista

    End Function

    Public Function GetUsuaris() As List(Of Usuari)
        Dim LLista As List(Of Usuari)

        Using Entities As LullabyEntities = New LullabyEntities()
            LLista = Entities.Usuaris.OrderBy(Function(f) f.Nom).ToList()
            LLista.Insert(0, New Usuari() With {.Id = -1, .Nom = "Tots"})
        End Using

        Return LLista

    End Function
End Class